package guowenlong.golf.ui.regist;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import guowenlong.golf.C;
import guowenlong.golf.R;
import guowenlong.golf.ui.regist.step_one.StepOneFragment;
import guowenlong.golf.ui.regist.step_three.StepThreeFragment;
import guowenlong.golf.ui.regist.step_two.StepTwoFragment;
import me.wenlong.mixutils.mvp.BaseActivity;

/**
 * des   : 注册页 author: Administrator email : guowenlong20000@gmail.com time  : 2017年02月09日 下午 5:35.
 */

public class RegistActivity extends BaseActivity<RegistPresenter, RegistModel> implements
    RegistContract.View {

  @BindView(R.id.tv_step1)
  TextView tvStep1;
  @BindView(R.id.tv_step2)
  TextView tvStep2;
  @BindView(R.id.tv_step3)
  TextView tvStep3;

  StepOneFragment mStepOneFragment;
  StepTwoFragment mStepTwoFragment;
  StepThreeFragment mStepThreeFragment;
  @BindView(R.id.ib_back)
  ImageButton ibBack;
  @BindView(R.id.fl_fragment_regist)
  FrameLayout flFragmentRegist;

  @Override
  protected int getContentView(Bundle savedInstanceState) {
    return R.layout.activity_regist;
  }

  @Override
  protected void initView(Bundle savedInstanceState) {
    selectFragment(C.FRAGMENT_REGIST_STEP1);
  }

  @Override
  protected void initData() {
    getSwipeBackLayout().setFinishAnchor(1000);
  }

  @OnClick(R.id.ib_back)
  public void onClick() {
    finish();
  }

  public void reload() {
    mStepOneFragment = null;
    mStepTwoFragment = null;
    mStepThreeFragment = null;
  }

  public void selectFragment(int fragmentNumber) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    hideFragments(transaction);
    switch (fragmentNumber) {
      case C.FRAGMENT_REGIST_STEP1:
        if (mStepOneFragment == null) {
          mStepOneFragment = new StepOneFragment();
          transaction.add(R.id.fl_fragment_regist, mStepOneFragment);
        }
        transaction.show(mStepOneFragment);
        break;
      case C.FRAGMENT_REGIST_STEP2:
        if (mStepTwoFragment == null) {
          mStepTwoFragment = new StepTwoFragment();
          transaction.add(R.id.fl_fragment_regist, mStepTwoFragment);
        }
        transaction.show(mStepTwoFragment);
        break;
      case C.FRAGMENT_REGIST_STEP3:
        if (mStepThreeFragment == null) {
          mStepThreeFragment = new StepThreeFragment();
          transaction.add(R.id.fl_fragment_regist, mStepThreeFragment);
        }
        transaction.show(mStepThreeFragment);
        break;
      default:
        break;
    }
    changeTextColor(fragmentNumber);
    transaction.commit();
  }

  public FragmentTransaction hideFragments(FragmentTransaction transaction) {
    if (mStepOneFragment != null) {
      transaction.hide(mStepOneFragment);
    }
    if (mStepTwoFragment != null) {
      transaction.hide(mStepTwoFragment);
    }
    if (mStepThreeFragment != null) {
      transaction.hide(mStepThreeFragment);
    }
    return transaction;
  }

  public void changeTextColor(int page_number) {
    tvStep1.setTextColor(ContextCompat.getColor(RegistActivity.this, R.color.front_light_dark));
    tvStep2.setTextColor(ContextCompat.getColor(RegistActivity.this, R.color.front_light_dark));
    tvStep3.setTextColor(ContextCompat.getColor(RegistActivity.this, R.color.front_light_dark));
    switch (page_number) {
      case C.FRAGMENT_REGIST_STEP1:
        tvStep1.setTextColor(ContextCompat.getColor(RegistActivity.this, R.color.front_green));
        break;
      case C.FRAGMENT_REGIST_STEP2:
        tvStep2.setTextColor(ContextCompat.getColor(RegistActivity.this, R.color.front_green));
        break;
      case C.FRAGMENT_REGIST_STEP3:
        tvStep3.setTextColor(ContextCompat.getColor(RegistActivity.this, R.color.front_green));
        break;
    }
  }
}
